package com.example.sharesinfo.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.sharesinfo.R;
import com.example.sharesinfo.model.ColumnTitle;
import com.example.sharesinfo.model.DataInfo;
import com.example.sharesinfo.model.StockInfo;
import com.example.sharesinfo.utils.LogUtils;
import com.kelin.scrollablepanel.library.PanelAdapter;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by kelin on 16-11-18.
 */

public class ScrollablePanelAdapter extends PanelAdapter {
    public static final int TITLE_TYPE = 4;
    public static final int STOCK_TYPE = 0;
    public static final int COLUMN_TITLE_TYPE = 1;
    public static final int DATA_TYPE = 2;
    private int type;
    private List<StockInfo> stockInfoList = new ArrayList<>();
    private List<ColumnTitle> columnTitleList = new ArrayList<>();
    private List<List<DataInfo>> dataList = new ArrayList<>();
    private OnBindDataListener onBindDataListener;

    public ScrollablePanelAdapter(int type, OnBindDataListener onBindDataListener) {
        this.onBindDataListener = onBindDataListener;
        this.type = type;
    }

    @Override
    public int getRowCount() {
        return stockInfoList.size() + 1;
    }

    @Override
    public int getColumnCount() {
        return columnTitleList.size();
    }


    public interface OnBindDataListener {
        void onBindViewHolder(RecyclerView.ViewHolder holder, int row, int column);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int row, int column) {
        onBindDataListener.onBindViewHolder(holder, row, column);

    }

    public int getItemViewType(int row, int column) {
        if (column == 0 && row == 0) {
            return TITLE_TYPE;
        }
        if (column == 0) {
            return STOCK_TYPE;
        }
        if (row == 0) {
            return COLUMN_TITLE_TYPE;
        }
        return DATA_TYPE;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case COLUMN_TITLE_TYPE:
                return new ColumnTitleViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.listitem_column_title, parent, false));
            case STOCK_TYPE:
                return new StockViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.listitem_stock_info, parent, false));
            case DATA_TYPE:
                return new DataViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.listitem_data_info, parent, false));
            case TITLE_TYPE:
                if (type == 0) {
                    return new TitleViewHolder(LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.listitem_title, parent, false));
                } else if (type == 1) {
                    return new TitleViewHolder(LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.pan_item_title, parent, false));
                }

            default:
                break;
        }
        return new DataViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listitem_data_info, parent, false));
    }


    public void setColumnTitleView(int pos, ColumnTitleViewHolder viewHolder) {
        ColumnTitle columnTitle = columnTitleList.get(pos - 1);
        if (columnTitle != null && pos > 0) {
            viewHolder.columnTitleTextView.setText(columnTitle.getColumnTitle());
        }
    }

    public void setStockView(int pos, StockViewHolder viewHolder) {
        StockInfo stockInfo = stockInfoList.get(pos - 1);
        if (stockInfo != null && pos > 0) {
            viewHolder.stockNameTextView.setText(stockInfo.getStockName());
            viewHolder.stockIdTextView.setText(stockInfo.getStockId());
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.i("点击事件");
            }
        });

    }

    public void setDataView(final int row, final int column, final DataViewHolder viewHolder) {
        final DataInfo dataInfo = dataList.get(row - 1).get(column - 1);
        viewHolder.dataView.setText(dataInfo.getData());

        //这里处理一下
        if (dataInfo != null) {
            dataInfo.setData(dataInfo.getData().replace("%", ""));
            dataInfo.setData(dataInfo.getData().replace("万", ""));
            dataInfo.setData(dataInfo.getData().replace("亿", ""));
            try {
                if (Double.valueOf(dataInfo.getData()) < 0) {
                    viewHolder.dataView.setTextColor(0xFF169619);
                    viewHolder.dataView.setText(dataInfo.getData());
                } else {
                    viewHolder.dataView.setTextColor(0xFFED382B);
                    viewHolder.dataView.setText(dataInfo.getData());
                }
            } catch (Exception e) {
                LogUtils.i("");
            }

        }
    }


    public static class ColumnTitleViewHolder extends RecyclerView.ViewHolder {
        public TextView columnTitleTextView;

        public ColumnTitleViewHolder(View itemView) {
            super(itemView);
            this.columnTitleTextView = itemView.findViewById(R.id.column_title);
        }

    }

    public static class StockViewHolder extends RecyclerView.ViewHolder {
        public TextView stockNameTextView;
        public TextView stockIdTextView;

        public StockViewHolder(View view) {
            super(view);
            this.stockNameTextView = (TextView) view.findViewById(R.id.stock_name);
            this.stockIdTextView = (TextView) view.findViewById(R.id.stock_id);
        }
    }

    public static class DataViewHolder extends RecyclerView.ViewHolder {
        public TextView dataView;
        public View view;

        public DataViewHolder(View view) {
            super(view);
            this.view = view;
            this.dataView = view.findViewById(R.id.stock_data);
        }
    }

    private static class TitleViewHolder extends RecyclerView.ViewHolder {
        public TextView titleTextView;

        public TitleViewHolder(View view) {
            super(view);
            this.titleTextView = view.findViewById(R.id.title);
        }
    }


    public void setStockInfoList(List<StockInfo> stockInfoList) {
        this.stockInfoList = stockInfoList;
    }

    public void setColumnTitleList(List<ColumnTitle> columnTitleList) {
        this.columnTitleList = columnTitleList;
    }

    public void setDataList(List<List<DataInfo>> dataList) {
        this.dataList = dataList;
    }

    public interface OnClickListener {
        void OnClick(int pos);
    }
}
